package me.dongcha.reggie402.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import me.dongcha.reggie402.common.R;
import me.dongcha.reggie402.entity.*;
import me.dongcha.reggie402.service.CategoryService;
import me.dongcha.reggie402.service.SetmealDtoService;
import me.dongcha.reggie402.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SetmealDtoService setmealDtoService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.warn("[添加套餐setmealDto]:{}", setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新建套餐成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        wrapper.orderByAsc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo, wrapper);

        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, setmealDtoPage);

        List<SetmealDto> records = new ArrayList<>();
        List<Setmeal> pageInfoRecords = pageInfo.getRecords();

        for (Setmeal setmeal : pageInfoRecords) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Long categoryId = setmeal.getCategoryId();
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName());
            records.add(setmealDto);
        }
        setmealDtoPage.setRecords(records);
        return R.success(setmealDtoPage);
    }

    @DeleteMapping
    public R<String> deleteByIdWithDish(@RequestParam("ids") List<Long> ids) {
        log.warn("[要删除的套餐数据是]:{}", ids);
        setmealService.deleteByIdWithDish(ids);
        return R.success("删除成功");
    }

    @GetMapping("/list")
    public R<List> list(Setmeal setmeal) {
        log.warn("[要查询的套餐分类id]:{}", setmeal.getCategoryId());

        String key = "setmeal_" + setmeal.getCategoryId();
        List<Setmeal> redissetmealList = (List<Setmeal>) redisTemplate.opsForValue().get(key);
        if (redissetmealList != null) {
            log.warn("[取数据的地方]:redis");
            return R.success(redissetmealList);
        }

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        List<Setmeal> setmeals = setmealService.list(wrapper);

        redisTemplate.opsForValue().set(key, setmeals);

        return R.success(setmeals);
    }
}
